//
//  SFButton.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/5/25.
//
//  自定义的按钮

import UIKit

enum SFButtonType {
    case txtUp          // 上文下图
    case txtLeft        // 左文右图
    case txtBottom      // 上图下文
    case txtRight       // 左图右文
}

class SFButton: UIView {
    
    open var imageView: UIImageView = UIImageView()
    open var titleLabel: UILabel = UILabel()
    open var isSelect: Bool = false{
        willSet {
            updateSubviews(state: newValue)
        }
    }
    open var space: CGFloat = 0 // 默认图文间距为 0
    open var buttonType: SFButtonType? {
        didSet {
            updateSubviewsLayout(type: buttonType ?? .txtLeft)
        }
    }
    
    private var imageCache: [String : UIImage]?
    private var titleCache: [String : String]?
    private var titleColorCache: [String : UIColor]?
    private var target: AnyObject?
    private var selector: Selector?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        isUserInteractionEnabled = true
        imageCache = [String : UIImage]()
        titleCache = [String : String]()
        titleColorCache = [String : UIColor]()
        
        addSubview(imageView)
        addSubview(titleLabel)
        
        titleLabel.textAlignment = .center
        imageView.contentMode = .scaleAspectFill
        
        buttonType = .txtLeft
        
        self.clickClosure = {[weak self] in
            guard let t = self?.target else { return }
            guard let s = self?.selector else { return }
            if t.responds(to: s) {
                t.performSelector(onMainThread: s, with: self, waitUntilDone: false)
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addTarget(target: AnyObject?, action: Selector) {
        guard let t = target else { return }
        self.target = t
        self.selector = action
    }
    
    func setImage(_ image: UIImage?, for: UIControl.State) {
        guard let img = image else { return }
        imageCache?.updateValue(img, forKey: String(`for`.rawValue))
        isSelect = false
    }
    
    func setTitle(_ title: String?, _ for: UIControl.State) {
        guard let t = title else { return }
        titleCache?.updateValue(t, forKey: String(`for`.rawValue))
        isSelect = false
    }
    
    func setTitleColor(_ color: UIColor?, _ for: UIControl.State) {
        guard let c = color else { return }
        titleColorCache?.updateValue(c, forKey: String(`for`.rawValue))
        isSelect = false
    }
    
    func updateSubviews(state: Bool) {
        let str = state ? String(UIControl.State.selected.rawValue) : String(UIControl.State.normal.rawValue)
        if imageCache?[str] != nil {
            imageView.image = imageCache?[str]
        }
        if titleCache?[str] != nil {
            titleLabel.text = titleCache?[str]
        }
        if titleColorCache?[str] != nil {
            titleLabel.textColor = titleColorCache?[str]
        }
    }
    
    func updateSubviewsLayout(type: SFButtonType) {
        switch type {
        
        case .txtUp:
            titleLabel.snp.makeConstraints { (make) in
                make.top.centerX.width.equalToSuperview()
            }
            imageView.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.centerY.equalTo(titleLabel.snp.bottom).offset(imgSize().height/2 + space)
            }
            
        case .txtLeft:
            titleLabel.snp.makeConstraints { (make) in
                make.height.left.centerY.equalToSuperview()
            }
            imageView.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.centerX.equalTo(titleLabel.snp.right).offset(imgSize().width/2 + space)
            }
            
        case .txtBottom:
            titleLabel.snp.makeConstraints { (make) in
                make.bottom.centerX.width.equalToSuperview()
            }
            imageView.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.centerY.equalTo(titleLabel.snp.top).offset(-imgSize().height/2 - space)
            }
            
        case .txtRight:
            titleLabel.snp.makeConstraints { (make) in
                make.height.right.centerY.equalToSuperview()
            }
            imageView.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.centerX.equalTo(titleLabel.snp.left).offset(-imgSize().width/2 - space)
            }
            
        }
    }
    
    func imgSize() -> CGSize {
        return imageView.image?.size ?? CGSize.zero
    }
    
}
